import random

import pygame.image

from actor import ShootDir, WalkDir
from actor.shoot_bullet import Bullet
from actor.shoot_soldier import ShootSoldier
from actor.xiao_tie import XiaoTie

"""
日军射击战士(上)
"""


class JapaneseArmySoldierUp(ShootSoldier):

    def __init__(self, x, y, xiao_tie):
        """
        参数初始化
        :param x:
        :param y:
        :param xiao_tie:
        """
        super(JapaneseArmySoldierUp, self).__init__("./resources/images/japanese/soldier1.png",
                                                    "resources/images/japanese/soldier1-1.png",
                                                    x, y, "日军射击战士", 100)
        self.xiao_tie = xiao_tie
        self.time_stamp = 10
        self.dir = WalkDir.up
        self.bullets = pygame.sprite.Group()
        self.shoot_action.shoot_dir = ShootDir.up
        self.walk_action.set_dir(WalkDir.up)

    def draw(self, surface, x: int, y: int):
        """
        绘制射击动作与子弹特效
        :param surface:
        :param x:
        :param y:
        :return:
        """
        if self.shoot_action.shoot_dir == ShootDir.up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - 60 - y))
            image = self.walk_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        for bullet in self.bullets:
            bullet.draw(surface, x - 45, y + 35)

    def run(self, down_flag, key_list, obstacle_group):
        """
        实现小铁与子弹的碰撞检测
        实现子弹的运动
        实现攻击时间间隔
        :param down_flag:
        :param key_list:
        :param obstacle_group:
        :return:
        """
        if self.time_stamp == 0:
            self.__create_bullet(self.pos_x, self.pos_y)
            self.time_stamp = 10
        else:
            self.time_stamp -= 1
        for bullet in self.bullets:
            bullet.run()
        pygame.sprite.groupcollide(self.bullets, obstacle_group, True, False)
        if pygame.sprite.spritecollide(self.xiao_tie, self.bullets, True):
            self.xiao_tie.hp -= random.randint(10, 20)

    def __create_bullet(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        bullet = Bullet(x, y, pygame.image.load("./resources/images/shoot/子弹.png"))
        bullet.set_dir(self.dir)
        self.bullets.add(bullet)


"""
日军射击战士(左)
"""


class JapaneseArmySoldierL(ShootSoldier):

    def __init__(self, x, y, xiao_tie):
        """
        参数初始化
        :param x:
        :param y:
        :param xiao_tie:
        """
        super(JapaneseArmySoldierL, self).__init__("./resources/images/japanese/soldier3.png",
                                                   "resources/images/japanese/soldier3-1.png",
                                                   x, y, "日军射击战士", 100)
        self.xiao_tie = xiao_tie
        self.time_stamp = 10
        self.shoot_action.shoot_dir = ShootDir.left_up
        self.walk_action.set_dir(WalkDir.left)
        self.dir = WalkDir.left
        self.bullets = pygame.sprite.Group()

    def draw(self, surface, x: int, y: int):
        """
        绘制射击动作与子弹特效
        :param surface:
        :param x:
        :param y:
        :return:
        """
        if self.shoot_action.shoot_dir == ShootDir.left_up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - y))
        for bullet in self.bullets:
            bullet.draw(surface, x - 35, y - 15)

    def run(self, down_flag, key_list, obstacle_group):
        """
        实现小铁与子弹的碰撞检测
        实现子弹的运动
        实现攻击时间间隔
        :param down_flag:
        :param key_list:
        :param obstacle_group:
        :return:
        """
        if self.time_stamp == 0:
            self.__create_bullet(self.pos_x, self.pos_y)
            self.time_stamp = 10
        else:
            self.time_stamp -= 1
        for bullet in self.bullets:
            bullet.run()
        pygame.sprite.groupcollide(self.bullets, obstacle_group, True, False)
        if pygame.sprite.spritecollide(self.xiao_tie, self.bullets, True):
            self.xiao_tie.hp -= random.randint(10, 20)

    def __create_bullet(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        bullet = Bullet(x, y, pygame.image.load("./resources/images/shoot/子弹.png"))
        bullet.set_dir(self.dir)
        self.bullets.add(bullet)


"""
日军射击战士(右)
"""


class JapaneseArmySoldierR(ShootSoldier):

    def __init__(self, x, y, xiao_tie: XiaoTie):
        """
        初始化参数
        :param x:
        :param y:
        :param xiao_tie:
        """
        super(JapaneseArmySoldierR, self).__init__("./resources/images/japanese/soldier2.png",
                                                   "resources/images/japanese/soldier2-1.png",
                                                   x, y, "日军射击战士", 100)
        self.dir = WalkDir.right
        self.xiao_tie = xiao_tie
        self.time_stamp = 10
        self.shoot_action.shoot_dir = ShootDir.right_up
        self.walk_action.set_dir(WalkDir.right)
        self.bullets = pygame.sprite.Group()

    def draw(self, surface, x: int, y: int):
        """
        绘制射击动作与子弹特效
        :param surface:
        :param x:
        :param y:
        :return:
        """
        if self.shoot_action.shoot_dir == ShootDir.right_up:
            image = self.shoot_action.get_image()
            surface.blit(image, (self.pos_x - x, self.pos_y - 60 - y))
        for bullet in self.bullets:
            bullet.draw(surface, x - 45, y + 35)

    def run(self, down_flag, key_list, obstacle_group):
        """
        实现小铁与子弹的碰撞检测
        实现子弹的运动
        实现攻击时间间隔
        :param down_flag:
        :param key_list:
        :param obstacle_group:
        :return:
        """
        if self.time_stamp == 0:
            self.__create_bullet(self.pos_x, self.pos_y)
            self.time_stamp = 10
        else:
            self.time_stamp -= 1
        for bullet in self.bullets:
            bullet.run()
        pygame.sprite.groupcollide(self.bullets, obstacle_group, True, False)
        if pygame.sprite.spritecollide(self.xiao_tie, self.bullets, True):
            self.xiao_tie.hp -= random.randint(10, 20)

    def __create_bullet(self, x, y):
        """
        实现埋地雷操作
        :param x:
        :param y:
        :return:
        """
        bullet = Bullet(x, y, pygame.image.load("./resources/images/shoot/子弹.png"))
        bullet.set_dir(self.dir)
        self.bullets.add(bullet)
